package com.ecs.server.importdata;

import java.io.File;
import java.util.List;
import com.ecs.server.importdata.XMLMessage;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class XMLMessageForm {
	
	static SAXBuilder builder;
	
	public XMLMessage formXMLMessage(File file) throws Exception
	{
		builder = new SAXBuilder();
		Document doc  = builder.build(file);
		return parseXML(doc);
	}
	
	private XMLMessage parseXML(Document doc)
	{
		XMLMessage xmlMessage = new XMLMessage();
		Element root  = doc.getRootElement();
		
		if(root.getName().equalsIgnoreCase("AuthenticationResponse"))
		{
			xmlMessage.setMessageType(root.getName());
			xmlMessage.setResponseCode(root.getAttributeValue("responseCode"));
		}
		else if(root.getName().equalsIgnoreCase("Response"))
		{
			xmlMessage.setMessageType(root.getName());
			xmlMessage.setResponseCode(root.getAttributeValue("responseCode"));
		}
		else if(root.getName().equalsIgnoreCase("SIDF"))
		{
			xmlMessage = SIDFparse(root, xmlMessage);
			xmlMessage.setMessageType(root.getName());
		}
		return xmlMessage;
	}
	
	private XMLMessage SIDFparse(Element root,XMLMessage xmlMessage)
	{
		Element Network = root.getChild("Network", root.getNamespace());
		String networkName = Network.getAttributeValue("name");
		xmlMessage.setNetworkId(networkName);
		
		Element Node = Network.getChild("Node",Network.getNamespace());
		String nodeName = Node.getAttributeValue("id");
		System.out.println("Node id is : " + nodeName);
		xmlMessage.setNodeId(nodeName);
		
		Element Sensor = Node.getChild("Sensor", Node.getNamespace());
		String sensorName = Sensor.getAttributeValue("id");
		xmlMessage.setSensorId(sensorName);
		
		Element Measurement = Sensor.getChild("Measurement",Sensor.getNamespace());
		String time = Measurement.getAttributeValue("time");
		String type = Measurement.getAttributeValue("quantity");
		System.out.println(type);
		xmlMessage.setMeasurementType(type);
		xmlMessage.setTime(time);
		
		
		@SuppressWarnings("unchecked")
		List<Element> list = Measurement.getChildren();
		
		for (int i = 0; i < list.size(); i++)
		{
			if(type.equalsIgnoreCase("Acceleration"))
			{
				if(i == 0){
					Element component = list.get(i);
					xmlMessage.setNormalValue(component.getValue());
				}else if(i == 1){
					Element component = list.get(i);
					xmlMessage.setPitch(component.getValue());
				}else if(i == 2){
					Element component = list.get(i);
					xmlMessage.setZ(component.getValue());
				}else if(i == 3){
					Element component = list.get(i);
					xmlMessage.setY(component.getValue());
				}else if(i == 4){
					Element component = list.get(i);
					xmlMessage.setX(component.getValue());
				}
			}
			else{
				Element component = list.get(i);
				System.out.println(component.getAttributeValue("unit"));
				xmlMessage.setNormalValue(component.getValue());
			}
		}
		return xmlMessage;
	}
	
	
	

}
